package com.example.patentback.service.impl;

import com.example.patentback.dao.PatentApplicantMapper;
import com.example.patentback.entity.Patent;
import com.example.patentback.entity.PatentApplicant;
import com.example.patentback.service.PatentApplicantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class PatentApplicantServiceImpl implements PatentApplicantService {

    @Autowired
    PatentApplicantMapper patentApplicantMapper;

    @Override
    public PatentApplicant selectByPatentId(Long id) {
        Example example = new Example(PatentApplicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("patentId", id);
        criteria.andEqualTo("contact", 1);
        return patentApplicantMapper.selectByExample(example).get(0);
    }

    @Override
    public int insertPatentApplicants(int contactId, Long patentId, List<Long> applicantIds) {
        List<PatentApplicant> patentApplicants = new ArrayList<>();
        for (int i = 0; i < applicantIds.size(); i++) {
            PatentApplicant patentApplicant = new PatentApplicant();
            patentApplicant.setApplicantId(applicantIds.get(i));
            patentApplicant.setPatentId(patentId);
            if (i == contactId) {
                patentApplicant.setContact(1L);
            } else {
                patentApplicant.setContact(0L);
            }
            patentApplicants.add(patentApplicant);
        }
        return patentApplicantMapper.insertList(patentApplicants);
    }

    @Override
    public List<PatentApplicant> selectByApplicantId(Long id) {
        Example example = new Example(PatentApplicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applicantId", id);
        return patentApplicantMapper.selectByExample(example);
    }

    @Override
    public List<PatentApplicant> selectPatents(Long id) {
        Example example = new Example(PatentApplicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("patentId", id);
        return patentApplicantMapper.selectByExample(example);
    }

    @Override
    public int updatePatentApplicant(List<Long> ids) {
        int len = ids.size() / 2;
        int total = 0;
        for (int i = 0; i < len; i++) {
            Example example = new Example(PatentApplicant.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("applicantId", ids.get(i));
            PatentApplicant patentApplicant = new PatentApplicant();
            patentApplicant.setApplicantId(ids.get(len + i));
            total += patentApplicantMapper.updateByExampleSelective(patentApplicant, example);
        }
        return total;
    }

    @Override
    public int deletePatentApplicant(List<Long> ids) {
        ids.forEach(id->{
            Example example = new Example(PatentApplicant.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("patentId", id);
            patentApplicantMapper.deleteByExample(example);
        });
        return ids.size();
    }
}
